package com.example.dbpractise1;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class PersonProvider extends ContentProvider {

	private static final String AUTHORITY = "com.example.providers.personprovider";
	private static final UriMatcher URIMATCHER = new UriMatcher(UriMatcher.NO_MATCH);
	private static final int PERSONS = 1; 
	private static final int PERSON = 2; 
	private DBOpenHelper dbOpenHelper;
	
	static {
		URIMATCHER.addURI(AUTHORITY, "person", PERSONS);
		URIMATCHER.addURI(AUTHORITY, "person/#", PERSON);
	}
	
	@Override
	public boolean onCreate() {
		
		dbOpenHelper = new DBOpenHelper(this.getContext());
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		
		switch (URIMATCHER.match(uri)) {
		case PERSONS:
			return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);

		case PERSON:
			long id = ContentUris.parseId(uri);
			String where = "_id=" + id;
			
			if (selection != null && !"".equals(selection)) {
				where = where + " and " + selection;
			}
			
			return db.query("person", projection, where, selectionArgs, null, null, sortOrder);
		default:
			throw new IllegalArgumentException("URI is Unknown: " + uri.toString());
		}
	}

	@Override
	public String getType(Uri uri) {
		switch(URIMATCHER.match(uri)) {
		case PERSONS:
			return "vnd.android.cursor.dir/person";
		case PERSON:
			return "vnd.android.cursor.item/person";
		default:
			throw new IllegalArgumentException("Uri Unkown:" + uri.toString());
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		
		switch (URIMATCHER.match(uri)) {
		case PERSONS:
			long id = db.insert("person", "amount", values);
			Uri insertUri = ContentUris.withAppendedId(uri, id);
			getContext().getContentResolver().notifyChange(uri, null);
			
			return insertUri;
		default:
			throw new IllegalArgumentException("URI is Unknown: " + uri.toString());
		}
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		
		switch(URIMATCHER.match(uri)) {
		case PERSONS:
			int num = db.delete("person", selection, selectionArgs);		
			
			if (num != 0) {
				getContext().getContentResolver().notifyChange(uri, null);
			}
			
			return num;
			
		case PERSON:
			long id = ContentUris.parseId(uri);
			String where = "_id=" + id;
			
			if (selection != null && !"".equals(selection)) {
				where = where + " and " + selection;
			}
			
			int num1 = db.delete("person", where, selectionArgs); 
			
			if (num1 != 0) {
				getContext().getContentResolver().notifyChange(uri, null);
			}			
			
			return num1;
		default:
			throw new IllegalArgumentException("Unknown uri: " + uri.toString());
		}
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		
		switch(URIMATCHER.match(uri)){
		
		case PERSON:
			long id = ContentUris.parseId(uri);
			String where = "_id=" + id;
			
			if (selection != null && !"".equals(selection)){
				where = selection + " and " + where;
			} 
			int num = db.update("person", values, where, selectionArgs);
			
			if (num != 0) {
				getContext().getContentResolver().notifyChange(uri, null);
			}
			
			return num;
			
		case PERSONS:
			int num1 = db.update("person", values, selection, selectionArgs);
			
			if(num1 != 0) {
				getContext().getContentResolver().notifyChange(uri, null);
			}
			
			return num1;	
		default:
			throw new IllegalArgumentException("Unknown uri: " + uri.toString());
		} 
	}

}
